# -*- coding:utf-8 -*-
from heuristics.sqr_min import *
from heuristics.Obst_Distance import *
from search.all import *
from multi_robot_problem import MultiRobotState
from time import *
from Tester import *
"""
Created on Apr 11, 2011

@author: bob
"""
#Kirshon Part
def createProblem():
    # First Problem - Easy

    robots = tuple([(9,0),(9,3)])
    
    dirt_locations = set()
    dirt_locations.add((0,1))
    dirt_locations.add((0,3))

    obstacle_locations = set()
    obstacle_locations.add((0,2))
    obstacle_locations.add((1,1))
    obstacle_locations.add((1,2))
    obstacle_locations.add((2,1))
    obstacle_locations.add((8,0))
    obstacle_locations.add((8,1))
    obstacle_locations.add((8,3))
    obstacle_locations.add((5,0))
    obstacle_locations.add((5,2))
    obstacle_locations.add((5,3))
    
    return MultiRobotState(10, 4, robots, frozenset(dirt_locations), frozenset(obstacle_locations))


''' algorithms initialization '''
algo_gbf_any = BestFirstAnyTimeGraphSearch()
algo_gbf = BestFirstGraphSearch()
algo_astar_any = AStarAnytime()
algo_astar = AStar()

'creating the problem'
width = 15
height = 15
robots = ((8,1),(2,3),(8,8),(5,5),(11,12))
dirt_locations = frozenset([(7,8),(7,1),(1,1),(2,5),(9,9),(5,2),(10,14)])
obstacles = frozenset([(0,4),(1,4),(2,4),(3,4),(4,4),(5,4),(6,4)])
#problem = MultiRobotState(width, height, robots, dirt_locations, obstacles)
#problem = createProblem()

'initializing random problems generator'
minWidth, maxWidth = 10,10
minHeight, maxHeight = 10,10
maxRobots, maxPiles = 3, 5
maxObstacles, obstacleType = 5, Tester.OBSTACLES_WALLS
wallsLenght, wallsVariance = 3, 3
pocketWidth, pocketWidthVariance = 3, 3
pocketHeight, pocketHeightVariance = 3, 3 
problemGen = Tester(minWidth, maxWidth,
                minHeight, maxHeight,
                maxRobots, maxPiles,
                maxObstacles, obstacleType,
                wallsLenght, wallsVariance,
                pocketWidth, pocketWidthVariance,
                pocketHeight, pocketHeightVariance)


tester = Tester(minWidth, maxWidth, minHeight, maxHeight, maxRobots, maxPiles, maxObstacles, \
                 obstacleType, wallsLenght, wallsVariance, pocketWidth, \
                 pocketWidthVariance, pocketHeight, pocketHeightVariance)
#problem = tester.generateProblemSpace()

#problem = createProblem()

#########################

#print "Anytime Algorithm:"
#obj = algo2.find(problem, Pile_Division(problem),20)
#print obj
#if not obj is None:
#    print "success"
#    start = clock()
#    obj = algo1.find(problem, Manhattan())
#    end = clock()
#    minutes = int((end - start) / 60)
#    print "Greedy:"
#    print minutes, (end-start) - minutes*60  
#    print obj
#    print "Length of solution: " , len(obj)
#else:
#    print "not enough time!"

#astarAnySol,sols = algo3.find(problem, Manhattan(), 60*2)
#if not (astarAnySol is None):
#    for node in astarAnySol.getPath():
#        print node
#    print "#solutions:",len(sols)
#    i = 0
#    for sol in sols:
#        print "sol ",i,":", len(sol.getPathActions())
#        i = i + 1
#else:
#    print "No solutions found!"
'''
sol = gbf.find(problem, Pile_Division(problem))
print len(sol),sol
'''

'''
for x in xrange(1,3):
    problem = tester.generateProblemSpace()
    print "~~~~~The problem:~~~~~"
    print problem
    astarAnySol, sols = algo_astar_any.find(problem, Pile_Division(problem), 10)
    if not (astarAnySol is None):
        print "number of sols:",len(sols),"sol length" ,len(astarAnySol.getPathActions())
        print astarAnySol.getPathActions()
        #    i = 0
        #    for sol in sols:
        #        print "sol ",i,":",len(sol.getPathActions()), sol.getPathActions()
        #        i = i + 1
    else:
            print "No solutions found!"
'''

for x in xrange(1,3):
    problem = tester.generateProblemSpace()
    print "~~~~~The problem:~~~~~"
    print problem
    astarAnySol= algo_astar.find(problem, Pile_Division(problem))
    if not (astarAnySol is None):
        print "sol length" ,len(astarAnySol)
        print astarAnySol
    else:
        print "No solutions found!"




